{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 载入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from urllib import request"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "target_url = (\"https://archive.ics.uci.edu/ml/machine-learning-\"\n",
    "\"databases/undocumented/connectionist-bench/sonar/sonar.all-data\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "req = request.Request(target_url, headers={\"User-Agent\": \"Mozilla/5.0\"})\n",
    "with request.urlopen(req) as resp:\n",
    "    data = resp.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_list = []\n",
    "labels = []\n",
    "\n",
    "# 注意，这样读进来的数据都是字符串\n",
    "for line in data.decode('utf-8').split('\\n'):\n",
    "    if not line.strip():\n",
    "        continue\n",
    "    \n",
    "    row = line.strip().split(',')\n",
    "    x_list.append(row)\n",
    "    labels.append(row[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Number of Rows:  208\n",
      "Number of Columns:  61\n"
     ]
    }
   ],
   "source": [
    "print(\"Number of Rows: \", len(x_list))\n",
    "print('Number of Columns: ', len(x_list[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查看各列数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "ncols = len(x_list[0])\n",
    "data_type = [0] * 3  # 统计每列中，数值、字符串、其他数据类型的数量\n",
    "col_data_type_counts = []  # 注意是对列进行统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "for col in range(ncols):\n",
    "    for row in x_list:\n",
    "        try:\n",
    "            a = float(row[col])\n",
    "            if isinstance(a, float):\n",
    "                data_type[0] += 1\n",
    "        except ValueError:\n",
    "            if len(row[col]) > 0:\n",
    "                data_type[1] += 1\n",
    "            else:\n",
    "                data_type[2] += 1\n",
    "    col_data_type_counts.append(data_type)  \n",
    "    data_type = [0] * 3  # 统计完一列后要重新进行初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Col#\tNumber\tStrings\t Other\n",
      "0\t208\t0\t0\n",
      "1\t208\t0\t0\n",
      "2\t208\t0\t0\n",
      "3\t208\t0\t0\n",
      "4\t208\t0\t0\n",
      "5\t208\t0\t0\n",
      "6\t208\t0\t0\n",
      "7\t208\t0\t0\n",
      "8\t208\t0\t0\n",
      "9\t208\t0\t0\n",
      "10\t208\t0\t0\n",
      "11\t208\t0\t0\n",
      "12\t208\t0\t0\n",
      "13\t208\t0\t0\n",
      "14\t208\t0\t0\n",
      "15\t208\t0\t0\n",
      "16\t208\t0\t0\n",
      "17\t208\t0\t0\n",
      "18\t208\t0\t0\n",
      "19\t208\t0\t0\n",
      "20\t208\t0\t0\n",
      "21\t208\t0\t0\n",
      "22\t208\t0\t0\n",
      "23\t208\t0\t0\n",
      "24\t208\t0\t0\n",
      "25\t208\t0\t0\n",
      "26\t208\t0\t0\n",
      "27\t208\t0\t0\n",
      "28\t208\t0\t0\n",
      "29\t208\t0\t0\n",
      "30\t208\t0\t0\n",
      "31\t208\t0\t0\n",
      "32\t208\t0\t0\n",
      "33\t208\t0\t0\n",
      "34\t208\t0\t0\n",
      "35\t208\t0\t0\n",
      "36\t208\t0\t0\n",
      "37\t208\t0\t0\n",
      "38\t208\t0\t0\n",
      "39\t208\t0\t0\n",
      "40\t208\t0\t0\n",
      "41\t208\t0\t0\n",
      "42\t208\t0\t0\n",
      "43\t208\t0\t0\n",
      "44\t208\t0\t0\n",
      "45\t208\t0\t0\n",
      "46\t208\t0\t0\n",
      "47\t208\t0\t0\n",
      "48\t208\t0\t0\n",
      "49\t208\t0\t0\n",
      "50\t208\t0\t0\n",
      "51\t208\t0\t0\n",
      "52\t208\t0\t0\n",
      "53\t208\t0\t0\n",
      "54\t208\t0\t0\n",
      "55\t208\t0\t0\n",
      "56\t208\t0\t0\n",
      "57\t208\t0\t0\n",
      "58\t208\t0\t0\n",
      "59\t208\t0\t0\n",
      "60\t0\t208\t0\n"
     ]
    }
   ],
   "source": [
    "# 输出统计结果。print函数会自动换行，所以不用拼‘\\n’\n",
    "print(\"Col#\" + '\\t' + \"Number\" + '\\t' + \"Strings\" + '\\t ' + \"Other\")\n",
    "for col, data_type in enumerate(col_data_type_counts):\n",
    "    print(str(col) + '\\t' + str(data_type[0]) + '\\t' + str(data_type[1]) + '\\t' + str(data_type[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
